package com.ray.core.exception;


import com.ray.common.http.lang.enums.HttpStatus;
import com.ray.common.http.lang.response.RayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 *
 * @author LiuBi
 * @version 1.0
 * @createDate 2024/5/28 14:57
 **/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 自定义全局异常
     *
     * @param rayException 异常信息
     * @return 错误结果对象实体类
     * @author liubi 2024/5/24 16:38
     **/
    @ResponseBody
    @ExceptionHandler(RayException.class)
    public RayResponse<Object> customException(RayException rayException) {
        log.error("系统异常{}", rayException.getMessage());
        return RayResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), rayException.getMessage(), null);
    }

    /**
     * 全局异常
     *
     * @param e 异常信息
     * @return 错误结果对象实体类
     * @author liubi 2024/5/24 16:38
     **/
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public RayResponse<Object> exception(Exception e) {
        log.error("系统异常{}", e.getMessage());
        return RayResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), null);
    }

}
